<template>
  <div class="login-container">
    <div class="login-box">
      <h2>后台管理系统登录</h2>
      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input 
            type="text" 
            id="username" 
            v-model="loginForm.username" 
            required 
            placeholder="请输入用户名"
          >
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input 
            type="password" 
            id="password" 
            v-model="loginForm.password" 
            required 
            placeholder="请输入密码"
          >
        </div>
        
        <button type="submit" :disabled="loading" class="login-btn">
          {{ loading ? '登录中...' : '登录' }}
        </button>
        
        <div v-if="error" class="error-message">
          {{ error }}
        </div>
      </form>
      
      <div class="register-link">
        <p>还没有账号？<a href="#" @click.prevent="showRegister = true">立即注册</a></p>
      </div>
    </div>
    
    <!-- 注册模态框 -->
    <div v-if="showRegister" class="modal-overlay" @click="showRegister = false">
      <div class="modal-content" @click.stop>
        <h3>用户注册</h3>
        <form @submit.prevent="handleRegister" class="register-form">
          <div class="form-group">
            <label for="reg-username">用户名</label>
            <input 
              type="text" 
              id="reg-username" 
              v-model="registerForm.username" 
              required 
              placeholder="请输入用户名"
            >
          </div>
          
          <div class="form-group">
            <label for="reg-email">邮箱</label>
            <input 
              type="email" 
              id="reg-email" 
              v-model="registerForm.email" 
              required 
              placeholder="请输入邮箱"
            >
          </div>
          
          <div class="form-group">
            <label for="reg-fullname">姓名</label>
            <input 
              type="text" 
              id="reg-fullname" 
              v-model="registerForm.fullName" 
              required 
              placeholder="请输入姓名"
            >
          </div>
          
          <div class="form-group">
            <label for="reg-password">密码</label>
            <input 
              type="password" 
              id="reg-password" 
              v-model="registerForm.password" 
              required 
              placeholder="请输入密码"
            >
          </div>
          
          <div class="form-actions">
            <button type="button" @click="showRegister = false" class="cancel-btn">取消</button>
            <button type="submit" :disabled="registerLoading" class="register-btn">
              {{ registerLoading ? '注册中...' : '注册' }}
            </button>
          </div>
          
          <div v-if="registerError" class="error-message">
            {{ registerError }}
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
import { authAPI } from '@api/auth.js'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      registerForm: {
        username: '',
        email: '',
        fullName: '',
        password: ''
      },
      loading: false,
      registerLoading: false,
      error: '',
      registerError: '',
      showRegister: false
    }
  },
  methods: {
    async handleLogin() {
      this.loading = true
      this.error = ''
      
      try {
            const response = await authAPI.login(this.loginForm)
        if (response.data.success) {
          // 登录成功，保存token和用户信息
          localStorage.setItem('jwt_token', response.data.token)
          this.$store.commit('setUser', response.data.user)
          this.$store.commit('setInitialized', true)
          this.$router.push('/admin')
        } else {
          this.error = response.data.error || '登录失败'
        }
      } catch (error) {
        this.error = error.response?.data?.error || '登录失败'
      } finally {
        this.loading = false
      }
    },
    
    async handleRegister() {
      this.registerLoading = true
      this.registerError = ''
      
      try {
            const response = await authAPI.register(this.registerForm)
        if (response.data.success) {
          this.showRegister = false
          this.registerForm = { username: '', email: '', fullName: '', password: '' }
          alert('注册成功！请使用新账号登录。')
        } else {
          this.registerError = response.data.error || '注册失败'
        }
      } catch (error) {
        this.registerError = error.response?.data?.error || '注册失败'
      } finally {
        this.registerLoading = false
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  min-height: calc(100vh - 64px);
}

.login-box {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  padding: 2rem;
  border-radius: 15px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.2);
  width: 100%;
  max-width: 400px;
  position: relative;
  overflow: hidden;
}

.login-box h2 {
  text-align: center;
  margin-bottom: 2rem;
  background: var(--gradient-rainbow);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  font-size: 2rem;
  font-weight: 700;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.login-form {
  margin-bottom: 1.5rem;
}

.form-group {
  margin-bottom: 1.2rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  color: #e0e0e0;
  font-weight: 500;
}

.form-group input {
  width: 100%;
  padding: 0.8rem;
  border: 2px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  font-size: 1rem;
  background: rgba(255, 255, 255, 0.1);
  color: #e0e0e0;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.form-group input:focus {
  outline: none;
  border-color: #97dc99;
  box-shadow: 0 0 0 3px rgba(151, 220, 153, 0.2);
  background: rgba(255, 255, 255, 0.15);
}

.login-btn {
  width: 100%;
  padding: 0.8rem;
  background: var(--gradient-primary);
  color: var(--text-white);
  border: none;
  border-radius: var(--radius-sm);
  font-size: 1rem;
  cursor: pointer;
  transition: all var(--transition-normal);
  font-weight: 600;
  position: relative;
  overflow: hidden;
}

.login-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left var(--transition-slow);
}

.login-btn:hover:not(:disabled)::before {
  left: 100%;
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: var(--shadow-glow-blue);
}

.login-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.error-message {
  color: var(--color-error);
  text-align: center;
  margin-top: var(--spacing-md);
  font-size: 0.9rem;
  font-weight: 500;
}

.register-link {
  text-align: center;
}

.register-link p {
  color: #e0e0e0;
}

.register-link a {
  color: #97dc99;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.register-link a:hover {
  color: #ffffff;
  text-decoration: underline;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  padding: 2rem;
  border-radius: 15px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  width: 90%;
  max-width: 500px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.modal-content h3 {
  margin-bottom: 1.2rem;
  background: var(--gradient-rainbow);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-align: center;
  font-weight: 700;
  font-size: 1.5rem;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.register-form {
  margin-bottom: 1rem;
}

.form-actions {
  display: flex;
  gap: 1rem;
  margin-top: 1.5rem;
}

.cancel-btn, .register-btn {
  flex: 1;
  padding: 0.8rem;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 1rem;
  cursor: pointer;
  transition: all var(--transition-normal);
  font-weight: 600;
}

.cancel-btn {
  background: #6c757d;
  color: var(--text-white);
}

.cancel-btn:hover {
  background: #5a6268;
  transform: translateY(-2px);
}

.register-btn {
  background: var(--gradient-secondary);
  color: var(--text-white);
}

.register-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: var(--shadow-glow-green);
}

.register-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
</style>
